import { defineStore } from 'pinia';
export const tagsStore = defineStore('tags-store', {
  state: () => {
    // return {
    //   nav: JSON.parse(sessionStorage.getItem('nav'))
    //     ? JSON.parse(sessionStorage.getItem('nav'))
    //     : [{ path: '/home', title: '首页', affix: '1', display: 1 }]
    // };
    null;
    null;
  },
  getters: {},
  actions: {
    // 存点击的路由
    updateDeviceCode(code) {
      this.deviceCode = code;
    },
    updateDeviceName(code) {
      console.log(code);
      this.deviceName = code;
    },
    setNavList(nav, faterNav) {
      if (this.nav.length > 0) {
        let navBs = this.nav.every((item) => {
          return item.path !== nav.path;
        });
        if (navBs) {
          this.nav.splice(1, 0, {
            path: nav.path,
            title: faterNav ? `${faterNav?.title}${faterNav?.title ? '-' : ''}${nav.title}` : nav.title
          });
        }
      } else {
        this.nav.push(nav);
      }
      this.setsessionStorageNav();
    },
    // 删除路由
    removeNavList(index) {
      this.nav.splice(index, 1);
      this.setsessionStorageNav();
    },
    // 关闭所有
    removeAllNav() {
      this.nav = [{ path: '/home', title: '首页', affix: '1', display: 1 }];
      this.setsessionStorageNav();
    },
    // 关闭其他
    removeOtherNav(nav) {
      this.nav = [{ path: '/home', title: '首页', affix: '1', display: 1 }];
      if (nav.path !== '/home') {
        this.nav.push(nav);
      }
      this.setsessionStorageNav();
    },
    // 关闭左侧
    removeLeftNav(index) {
      this.nav.splice(1, index - 1);
      this.setsessionStorageNav();
    },
    // 关闭右侧
    removeRightNav(index) {
      this.nav.splice(index + 1, this.nav.length - index);
      this.setsessionStorageNav();
    },
    setsessionStorageNav() {
      sessionStorage.setItem('nav', JSON.stringify(this.nav));
    }
  }
});
